package org.opu.pc.services;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import org.opu.pc.sg.StatementGenerationServices;

/**
 *
 * http://www.assembla.com/spaces/ap-071/wiki/%D0%93%D0%B5%D0%BD%D0%B5%D1%80%D0%B0%D1%86%D0%B8%D1%8F_%D0%B2%D0%B5%D0%B4%D0%BE%D0%BC%D0%BE%D1%81%D1%82%D0%B5%D0%B9
 * @author Yuriy Dunko
 */
public class StatementGenerationUserServiceImpl
        implements StatementGenerationUserService {

    private int currentYear;
    private StatementGenerationServices services;

    public StatementGenerationUserServiceImpl(StatementGenerationServices services) {
        this.services = services;
        
        // We will not generate statement in NewYear.
        this.currentYear = currentYear();
    }
    
    @Override
    public void departmentPublicationsStudyStatement(File file)
            throws IOException {
        st1(file, currentYear);
    }
    
    @Override
    public void departmentPublicationsFinancialStatement(File file)
            throws IOException {
        st1(file, currentYear - 1);
    }

    @Override
    public void departmentPublicationsProfessorsStatement(File file)
            throws IOException {
        st1(file, currentYear - 2);
    }

    @Override
    public void studentScienceStatement(File file)
            throws IOException {
        validateFile(file);
        services.studentScienceStatement(firstDay(currentYear),
                lastDay(currentYear), new FileOutputStream(file));
    }

    /**
     *
     * @param file the file where to need to store statement
     * @param professorId is id of the author
     */
    @Override
    public void professorScienceStatement(File file, int professorId)
            throws IOException {
        validateFile(file);
        services.professorScienceStatement(firstDay(currentYear),
                lastDay(currentYear), professorId, new FileOutputStream(file));
    }

    private void st1(File file, int year) throws IOException {
        validateFile(file);
        services.departmentPublicationsStatement(firstDay(year),
                lastDay(currentYear), new FileOutputStream(file));
    }

    private void validateFile(File file) throws IllegalArgumentException {
        if (file == null) {
            throw new IllegalArgumentException("File can't be null");
        }
        
        if (!file.getParentFile().exists()) {
            file.getParentFile().mkdirs();
        }
    }

    private int currentYear(){
        // TODO: Yuriy Dunko: move this method to some 'util'.
        return new GregorianCalendar().get(Calendar.YEAR);
    }

    /**
     * Generate date, that are represent first day at the year
     * @param year
     * @return the first day of the year
     */
    private Date firstDay(int year){
        // TODO: Yuriy Dunko: move this method to some 'util'.
        return new GregorianCalendar(year, Calendar.JANUARY, 1).getTime();
    }

    /**
     * Generate date, that are represent last day at the year
     * @param year
     * @return the last day of the year
     */
    private Date lastDay(int year){
        // TODO: Yuriy Dunko: move this method to some 'util'.
        return new GregorianCalendar(year, Calendar.DECEMBER, 31).getTime();
    }
}
